package org.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
public class WordCount extends Configured implements Tool {
    public static void main(String[] args) throws Exception {
        int result = ToolRunner.run(new WordCount(), args);
        System.exit(result);
    }
    //声明嗠顺地址
    private static String server = "hdfs://server201:8020";
    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println("usage: " + this.getClass().getSimpleName() + " <inPath> <outPath>");
            ToolRunner.printGenericCommandUsage(System.out);
            return -1;
        }
        Configuration config = getConf();
        config.set("fs.defaultFS", server);
        //指定resourcemanger的地址
        config.set("yarn.resourcemanager.hostname", "server201");
        config.set("dfs.replication", "1");
        config.set("dfs.permissions.enabled", "false");
        FileSystem fs = FileSystem.get(config);
        Path dest = new Path(server + args[1]);
        if (fs.exists(dest)) {
            fs.delete(dest, true);
        }
        Job job = Job.getInstance(config,"WordCount");
        job.setJarByClass(getClass());
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        FileInputFormat.addInputPath(job, new Path(server + args[0]));
        FileOutputFormat.setOutputPath(job, dest);
        boolean boo = job.waitForCompletion(true);
        return boo ? 0 : 1;
    }

    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        private LongWritable count = new LongWritable(1);
        private Text text = new Text();
        @Override
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String str = value.toString();
            String[] strs = str.split("\\s+");
            for (String s : strs) {
                text.set(s);
                context.write(text, count);
            }
        }
    }

    public static class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
        @Override
        public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long sum = 0;
            for (LongWritable w : values) {
                sum += w.get();
            }
            context.write(key, new LongWritable(sum));
        }
    }
}